-
-
Notifications
You must be signed in to change notification settings - Fork 32.9k
gh-71810: fix corner case (length==0) for int.to_bytes() #138739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
```pycon >>> (0).to_bytes(0, 'big', signed=True) b'' >>> (-1).to_bytes(0, 'big', signed=True) # was b'' Traceback (most recent call last): File "<python-input-0>", line 1, in <module> (-1).to_bytes(0, 'big', signed=True) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ OverflowError: int too big to convert ```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This comment was marked as resolved.
This comment was marked as resolved.
@serhiy-storchaka: I would suggest to backport this change to Python 3.13 and 3.14. What do you think? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems there are no enough tests for signed=True
. Please add also tests for (128).to_bytes(0, 'big', signed=True)
and (-129).to_bytes(0, 'big', signed=True)
.
Disclaimer: I was thinking about |
Or better add several tests for extreme values and values just above the limit for all combination of signedness and n=0, 1, 2. |
Done. There are such tests, but rather for "automatic" value for length parameter (determined by result): Line 1262 in 1ce0553
It seems to be a big refactoring of tests. Maybe it does make sense as a separate pr? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add much more tests. Anyway, LGTM. 👍
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Thanks @skirpichev for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14. |
…nGH-138739) ```pycon >>> (0).to_bytes(0, 'big', signed=True) b'' >>> (-1).to_bytes(0, 'big', signed=True) # was b'' Traceback (most recent call last): File "<python-input-0>", line 1, in <module> (-1).to_bytes(0, 'big', signed=True) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ OverflowError: int too big to convert ``` (cherry picked from commit 011179a) Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
…nGH-138739) ```pycon >>> (0).to_bytes(0, 'big', signed=True) b'' >>> (-1).to_bytes(0, 'big', signed=True) # was b'' Traceback (most recent call last): File "<python-input-0>", line 1, in <module> (-1).to_bytes(0, 'big', signed=True) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ OverflowError: int too big to convert ``` (cherry picked from commit 011179a) Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
GH-138782 is a backport of this pull request to the 3.14 branch. |
GH-138783 is a backport of this pull request to the 3.13 branch. |
Merged, thank you. |
…38739) (#138783) gh-71810: Fix corner case (length==0) for int.to_bytes() (GH-138739) ```pycon >>> (0).to_bytes(0, 'big', signed=True) b'' >>> (-1).to_bytes(0, 'big', signed=True) # was b'' Traceback (most recent call last): File "<python-input-0>", line 1, in <module> (-1).to_bytes(0, 'big', signed=True) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ OverflowError: int too big to convert ``` (cherry picked from commit 011179a) Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@@ -1209,7 +1209,7 @@ _PyLong_AsByteArray(PyLongObject* v, | |||
*p = (unsigned char)(accum & 0xff); | |||
p += pincr; | |||
} | |||
else if (j == n && n > 0 && is_signed) { | |||
else if (j == n && is_signed) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change introduced an undefined behavior: unsigned char msb = *(p - pincr);
read is out of the bounds if n == 0. I wrote #138873 to fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, sorry. Thanks!
Uh oh!
There was an error while loading. Please reload this page.